package jiubeirobot.com.robotshop.ui.activity.shop;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fecmobile.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseActivity;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.BaseData;
import jiubeirobot.com.robotshop.bean.StockBean;
import jiubeirobot.com.robotshop.ui.adapter.ShopCartAdapter;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.PageUtils;

/**
 * @date 创建时间:2017/8/22
 * @author TheJoker丶豪
 * 购物车的页面
 *
 */
public class ShoppingCartActivity extends BaseActivity implements XRecyclerView.LoadingListener, ShopCartAdapter.OnCustomNumInputTextChange {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_back_txt)
    TextView tvBackTxt;
    @BindView(R.id.iv_search_icon)
    ImageView ivSearchIcon;
    @BindView(R.id.btn_settlement)
    Button btnsettlement;
    @BindView(R.id.rv_shop_item)
    XRecyclerView rvshop_item;
    @BindView(R.id.tv_shop_total_money)
    TextView tvShopTotalMoney;
    @BindView(R.id.tv_total_num)
    TextView tvTotalNum;
    @BindView(R.id.tv_be_paid_money)
    TextView tvBePaidMoney;

    private PageUtils pageUtils = new PageUtils();
    ShopCartAdapter shopCartAdapter;
    List<StockBean> cartShops = BaseData.getBaseData().getCardShops();


    @Override
    public int pageLayout() {
        return R.layout.activity_shopping_cart;
    }

    @Override
    public void initTitle() {
        tvBackTxt.setVisibility(View.GONE);
        ivSearchIcon.setVisibility(View.INVISIBLE);
        tvTitle.setText(getString(R.string.common_buy_card));
    }

    @Override
    public void initView() {
        rvshop_item.setPullRefreshEnabled(false);
        rvshop_item.setLayoutManager(new LinearLayoutManager(this));
        Drawable dividerDrawable = ContextCompat.getDrawable(this, com.fecmobile.xrecyclerview.R.drawable.divider_sample);
        rvshop_item.addItemDecoration(rvshop_item.new DividerItemDecoration(dividerDrawable));
        shopCartAdapter = new ShopCartAdapter(this, cartShops);
        rvshop_item.setAdapter(shopCartAdapter);
        shopCartAdapter.setOnCustomNumInputTextChange(this);
        setPrice();

    }

    @Override
    protected boolean isOpenStatus() {
        return super.isOpenStatus();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseData.getBaseData().getCardItemNum() == 0) {
//            finish();
        } else {
            shopCartAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 设置价格
     */
    private void setPrice() {
        tvShopTotalMoney.setText("￥" + BaseData.getBaseData().getTotalMoney());
        tvTotalNum.setText(BaseData.getBaseData().getCardItemNum() + "件商品");
        tvBePaidMoney.setText("￥" + BaseData.getBaseData().getTotalMoney());
    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {

    }

    @OnClick({R.id.llyt_back,R.id.btn_settlement})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.llyt_back:
                finish();
                break;
            case R.id.btn_settlement:
                //跳转至核算订单
                if (BaseData.getBaseData().getCardItemNum() == 0) {
                    Log.d("print", "onclick: " + BaseData.getBaseData().getCardItemNum());
//                    return;
                }
                Activitys.toCheckOrder(ShoppingCartActivity.this,null);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        pageUtils.setCurentLoadMode(false);
    }

    @Override
    public void onLoadMore() {
        pageUtils.setCurentLoadMode(true);
    }

    /**
     * 监听购物车里面输入框值改变的方法
     *
     * @param v
     * @param num
     * @param bean
     */
    @Override
    public void textChange(View v, int num, StockBean bean) {
        BaseData.getBaseData().setShopNum(bean, num);
        setPrice();
    }

    @Override
    public void delete(View v, StockBean bean, int postion) {
        L.i("--------" + postion);
        BaseData.getBaseData().removeShop(bean);
        if (BaseData.getBaseData().getCardItemNum() == 0) {
            finish();
        } else {
            setPrice();
            shopCartAdapter.notifyDataSetChanged();
        }
    }
}
